package com.ww.springboot.boot.baseTest.sync.volatil;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2025-01-23 09:15
 */
public class Demo2 {

    private static int value = 0;
    private static boolean flag = false;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            flag = true;
            int i = 21 + 21;
            value = i;
        });

        for (int i = 0; i < 100; i++) {
            Thread t2 = new Thread(() -> {
                while (true) {
                    if (flag && value != 42) {
                        System.out.println(Thread.currentThread() + ":" + flag + "   " + value);
                    }
                }
            });
            t2.setDaemon(true);
            t2.start();
        }


        t1.start();
        System.out.println("===");
        Thread.sleep(1000);
        System.out.println("=====");
    }

}
